package com.bjpowernode.crm.service.impl;

import com.bjpowernode.crm.constants.Constants;
import com.bjpowernode.crm.mapper.UserMapper;
import com.bjpowernode.crm.pojo.User;
import com.bjpowernode.crm.service.UserService;
import com.bjpowernode.crm.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    private String checkUser(User user, String userIp) {
        if (user == null) {
            return Constants.Message.ACT_PWD_ERR;
        }

        String expireTime = user.getExpireTime();
        if (expireTime != null && !"".equals(expireTime)) {
            // now:         2021-11-04
            // expireTime   2021-11-14
            long now = System.currentTimeMillis();
            try {
                long expire = Constants.DateFormat.SDF19.parse(expireTime).getTime();
                if (now > expire) {
                    return Constants.Message.ACT_GQ;
                }
            } catch (ParseException e) {
                return Constants.Message.ACT_GQ2;
            }
        }

        if (!"1".equals(user.getLockStatus())) {
            return Constants.Message.ACT_LOCK;
        }

        // userIp: 127.0.0.1
        // allowIps: 127.0.0.2,192.168.137.100,127.0.0.1
        String allowIps = user.getAllowIps();
        if (allowIps != null && !"".equals(allowIps)) {
            String[] ipArr = allowIps.split(",");
            boolean allow = false;
            for (String ip : ipArr) {
                if (ip.equals(userIp)) {
                    allow = true;
                    break;
                }
            }
            if (!allow) {
                return Constants.Message.IP_UN_ALLOW;
            }
        }
        return null;
    }

    public User getUser(String username, String password, String userIp) {

        // 数据库中是加密的结果，因此此处需要先加密再进行查询
        password = MD5Utils.getMD5(password);

        User user = userMapper.getUser(username, password);
        String errMsg = checkUser(user, userIp);
        if (errMsg != null) {
            throw new RuntimeException(errMsg);
        }

        return user;
    }

    public User getUserForAuto(String username, String password, String userIp) {
        User user = userMapper.getUser(username, password);

        String errMsg = checkUser(user, userIp);
        if (errMsg != null) {
            return null;
        }

        return user;
    }

    public void changePwd(String oldPwd, String newPwd, String userId) {
        oldPwd = MD5Utils.getMD5(oldPwd);

        User user = userMapper.getById(userId);

        if (!oldPwd.equals(user.getLoginPwd())) {
            throw new RuntimeException(Constants.Message.ERR_OLDPWD);
        }
        if (newPwd == null || "".equals(newPwd)) {
            throw new RuntimeException(Constants.Message.EMPTY_PWD);
        }

        newPwd = MD5Utils.getMD5(newPwd);
        userMapper.changePwd(newPwd, user.getId());

    }

    public List getOwners() {
        return userMapper.getOwners();
    }
}
